<?php


namespace app\admin\middleware;

use app\admin\model\ListsFieldsDisplay;
use think\facade\Config;
use think\facade\Db;
use think\App;

class TableFields
{
    public function handle($request, \Closure $next)
    {
        $response = $next($request);

        //列表返回 字段
        $action = ['recycle', 'index', 'lists'];
        if(in_array($request->action(), $action)){
            $controller = $request->controller();
            //处理多级控制器，“.”问题
            if(strpos($controller, '.') !== false)
            {
                $model_name_array = explode('.', $controller);
                $controller = $model_name_array[0];
            }

            $table = strtolower(preg_replace('/(?!^)[A-Z]/', '_$0', $controller));
            $prefix = Config::get('database.connections.mysql.prefix');
            $fields = Db::getFields($prefix . $table);

            //获取不需要显示字段
            $modelName = 'app\\admin\\controller\\'.$controller;
            $model = new $modelName(app());
            $not_display_fields = explode(',', $model->index_not_display_fields);


            //用户保存的后端显示字段
            $list_fields = ListsFieldsDisplay::where('user_id', $request->user_id)->where('controller', $request->controller())->value('fields');
            if(!empty($list_fields))
            {
                $list_fields = explode(',', $list_fields);
            }else{
                $list_fields = [];
            }

            $fields = array_merge($fields, $model->additional_fields_list);
            $field_array = [];
            foreach($fields as $k=>$v){
                //过滤不需要显示的字段
                if(!in_array($v['name'], $not_display_fields))
                {
                    //字段描述中只显示_之前信息
                    $strpos = strpos($v['comment'], '_');
                    if($strpos)
                    {
                        $v['comment'] = substr($v['comment'],0,$strpos);
                    }

                    $is_select = 0;
                    //判断字段是否被选择
                    if(!empty($list_fields))
                    {
                        if(in_array($v['name'], $list_fields))
                        {
                            $is_select = 1;
                        }
                    }
                    $field_array[] = [
                        'name' => $v['name'],
                        'comment' => $v['comment'],
                        'is_select' => $is_select
                    ];
                }
            }
            //只显示用户指定的字段
            $data = $response->getData();
            //为空显示全部，反之显示指定字段
            if(empty($list_fields))
            {
                if(is_array($data))
                {
                    $new_data = $data['data']['list'];
                }
            }else{
                //id一直显示
                if(!in_array('id', $list_fields))
                {
                    $list_fields[] = 'id';
                }
                $new_data = [];

                if(is_array($data))
                {
                    foreach($data['data']['list'] as $k=>$v)
                    {
                        $new_data[$k] = [];
                        foreach($list_fields as $key=>$val)
                        {
                            if(!empty($v[$val]))
                            {
                                $new_data[$k] = array_merge([$val => $v[$val]], $new_data[$k]);
                            }
                        }
                    }
                }
            }
            if(is_array($data)){
                $data['data']['fields'] = $field_array;
                $data['data']['list'] = $new_data;
                $response->data($data);
            }
        }

        return $response;
    }


}